(()=>{
	let lastViewPage = null;
	let currentViewPage = null;
	
	window.BlockHelper = class BlockHelper {
		static render(block) {
			if(window.SearchController && SearchController.isInSearch()) {
				block.searchMatch = !!SearchController.isMatchBlock(block.getId());
			}
			
			let type = block.getType() ? block.getType() : BlockModel.TYPE.TEXT;
			let templatePath = `page/home/block/type/${type}.html`;
			
			if(!Core.srcFileExists(templatePath)) {
				templatePath = `page/home/block/type/text.html`;
			}
			
			block.originValue = block.getValue('');
			
			if(window.SearchController && SearchController.isInSearch()) {
				block.setLabel(SearchController.hilightKeywords(block.getLabel()));
				block.setComment(SearchController.hilightKeywords(block.getComment()));
				
				if(block.getType() != BlockModel.TYPE.PASSWORD) {
					block.setValue(SearchController.hilightKeywords(block.getValue()));
				}
			} else {
				block.setLabel(Core.htmlEncode(block.getLabel()));
				block.setComment(Core.htmlEncode(block.getComment()));
				
				if(block.getType() != BlockModel.TYPE.PASSWORD) {
					block.setValue(Core.htmlEncode(block.getValue('')));
				} else {
					block.setValue(block.getValue(''));
				}
			}
			
			return Core.renderSrcFile(templatePath, block);
		}
		
		static copyValueById(id) {
			let m = new BlockModel;
			m.load(id);
			CoreHelper.copyValueAndTips(m.getValue());
		}

		static copyLabelById(id) {
			let m = new BlockModel;
			m.load(id);
			CoreHelper.copyValueAndTips(m.getLabel());
		}
		
		static copyBlockElementLabelById(id) {
			CoreHelper.copyElementValue(CoreHelper.getBlockLabelElement(id));
		}
		
		static exportBlock(id) {
			if(!Core.isNumber(id)) {
				id = $(id).data('blockId');
			}
			
			if(!id) {
				return ;
			}
			let model = new BlockModel();
			model.setId(id).load();
			
			if(!model.getId()) {
				CoreHelper.alertError('此记录不存在');
				return;
			}
			
			CoreHelper.copyValueAndTips(model.toString());
		}
		
		static deleteBlock(id) {
			if(!Core.isNumber(id)) {
				id = $(id).data('blockId');
			}
			
			if(!id) {
				return ;
			}
			let model = new BlockModel();
			model.setId(id).load();
			
			if(model.getId()) {
				if(!confirm(`确定要删除 '${model.getLabel()}' 吗?`)) {
					return;
				}
				
				let blockCollector = model.collector();
				let data = blockCollector.where('parent', model.getId()).load().get();
				
				for(let i=0; i<data.length; i++) {
					data[i].delete();
				}
				
				model.delete();
			}
			
			if(model.ok()) {
				CoreHelper.getBlockElement(id).remove();
			} else {
				CoreHelper.msg('删除失败');
			}
		}
		
		static setFrontendTitle(blockId, title) {
			CoreHelper.getBlockElement(blockId).find('.title-content').html(Core.htmlEncode(title));
		}
		
		static updateFrontendHtml(blockId, html) {
			let targetBlock = CoreHelper.getBlockElement(blockId);
			let content = $(html);
			targetBlock.html(content.html());
		}
		
		static togglePasswordShow(id) {
			let show = $('#block_value_show_' + id);
			let pwd = $('#block_value_' + id);
			
			if(show.css('display') == 'none') {
				show.css('display', 'inline-block');
				pwd.css('display', 'none');
			} else {
				show.css('display', 'none');
				pwd.css('display', 'inline-block');
			}
		}
		
		static moveUp(id) {
			return this.move(id, -1);
		}
		
		static moveDown(id) {
			return this.move(id, 1);
		}
		
		static move(id, direction) {
			if(!Core.isNumber(id)) {
				id = $(id).data('blockId');
			}
			
			let block = CoreHelper.getBlockElement(id);
			let model = new BlockModel;
			model.load(id);
			
			if(!model.getId()) {
				CoreHelper.alertError('此记录不存在');
				return;
			}
			
			let targetBlock = null;
			
			if(direction == 1) {
				targetBlock = block.next();
			} else if(direction == -1) {
				targetBlock = block.prev();
			} else {
				return;
			}
			
			if(!targetBlock.length) {
				return;
			}
			
			let targetId = targetBlock.attr('id');
			let targetHtml = targetBlock.html();
			let targetBlockId = targetBlock.attr('data-block-id');
			
			targetBlock.html(block.html());
			targetBlock.attr('id', block.attr('id'));
			targetBlock.attr('data-block-id', block.attr('data-block-id'));
			
			block.html(targetHtml);
			block.attr('id', targetId);
			block.attr('data-block-id', targetBlockId);
			
			let bros = block.parent().children();
			
			for(let i=0; i<bros.length; i++) {
				let curid = $(bros[i]).attr('data-block-id');
				(new BlockModel).setId(curid).setPosition(i+1).save();
			}
		}

	};
	
	Core.setToParentWindow('BlockHelper', BlockHelper);
})();